/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package _examples;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Random;
import jbotsim.Node;
import java.util.Vector;

/**
 *
 * @author vincent
 */
public class team {
   public Vector<Node> teamv;
    /**
     *
     */
    
   public Vector<Node> teamv2;
    public Vector<Point2D.Double> targets;
   public int movetype=0;
   boolean inmove=false;
   double remx;
   double remy;
   boolean inited=false;
   double lmax=-1;
   public int index=0;
   public team(int in)
    {
        index=in;
       remx=-1;
       inmove=false;
        teamv=new Vector<Node>();
         teamv2=new Vector<Node>();
         targets=new  Vector<Point2D.Double>();
    }
   public void addnode(Node n)
   {
       teamv.add(n);
   }
    public void addnode2(Node n)
   {
       teamv2.add(n);
   }
   public void teammove(double x, double y, int step)
   {
     double x1=teamv.get(0).getX();
       double y1=teamv.get(0).getY();
       double r=Math.sqrt(Math.pow(x-x1,2)+Math.pow(y-y1,2));
       if(movetype==0)
       {
           
       }
       else if(movetype==1)
       {
           //平移 此时x和y是平移参数
           
           
           
           int i=0;
           for(i=0;i<teamv.size();i++)
           {
               //teamv2.get(i).inmove=true;
                teamv2.get(i).setLocation(teamv.get(i).getX()+x, teamv.get(i).getY()+y);
                  teamv.get(i).setLocation(teamv.get(i).getX()+x, teamv.get(i).getY()+y);
             //  Point2D.Double target = new Point2D.Double( teamv.get(i).getX()+x0,  teamv.get(i).getY()+y0);
           //   teamv.get(i).setDirection(target);
            //   teamv.get(i).move(step);
           }    
       }
    /*        else if(movetype==98)
       {
           //平移 此时x和y是平移参数

                 int i;
         
 
                   
               if(inmove==false)
               {
                  targets.clear();
                  inmove=true;
                      for(i=0;i<teamv.size();i++)
                  {
                   Point2D.Double target = new Point2D.Double(x-teamv.get(0).getX()+teamv.get(i).getX(),y-teamv.get(0).getY()+teamv.get(i).getY());
                   targets.add(target);
                  }
                   
               }
                      
           for(i=0;i<teamv.size();i++)
           {
  
                   teamv.get(i).setDirection(targets.get(i));
                   teamv.get(i).move(0.3);
                     teamv2.get(i).setDirection(targets.get(i));
                   teamv2.get(i).move(0.3);
  
           }    
      
                 
              for(i=0;i<teamv.size();i++)
           {
               int isfind=0;
                 if(teamv.get(i).distance(targets.get(i))>step)
           {
               
               isfind=1;
               return;
           } 
                 
                 }
                       inmove=false;
                      targets.clear();
                       global.jbm.donext(index);
                     
                       
            
       
       }  */
       else  if(movetype==4)
       {
           //stop
           int i=0;
            
       }
       else if(movetype==2)
       {
           int i;
            double tx=teamv.get(0).getX();
              double ty=teamv.get(0).getY();
                  if(tx<=x&&ty<=y)
                {
                   int mm=0;
                }
            
   
          double steprot=(double)step/1000;
             for(i=0;i<teamv.size();i++)
           {
                teamv2.get(i).inmove=true;
                 tx=teamv.get(i).getX();
                ty=teamv.get(i).getY();
                 double l=Math.sqrt(Math.pow(tx-x,2)+Math.pow(ty-y,2));
                 if(teamv.get(i).rot==-1)
                 {
                     double rot=Math.asin((ty-y)/l);
                     
                     if(tx>x&&ty>y)
                     {
                      //1
                        teamv.get(i).rot=rot;
                     }
                     else if(tx>x&&ty<y)
                     {
                         //4
                      teamv.get(i).rot=rot;
                     }
                      else if(tx<x&&ty>y)
                     {
                         //2
                          teamv.get(i).rot=rot+3.14;
                     }
                      else if(tx<x&&ty<y)
                     {
                         //3
                        teamv.get(i).rot=rot+3.14;
                     }
                 }
                 else
                 {
                     
                 }
           //  double rot=Math.asin((ty-y)/l);
             //rot=Math.abs(rot);
             
               
              teamv.get(i).rot=teamv.get(i).rot+steprot;
                double dx=l * Math.cos( teamv.get(i).rot);
            
             
               double dy=l * Math.sin( teamv.get(i).rot);
        
        /*       double l=Math.sqrt(Math.pow(tx-x,2)+Math.pow(ty-y,2));
               double dx=Math.pow(step,2)/2/l;
               double dy=Math.sqrt(Math.pow(step,2)-Math.pow(dx,2));  */
             
            tx=x+dx;
            ty=y+dy;
               teamv.get(i).setLocation(tx, ty);
               
           teamv2.get(i).setLocation(tx, ty);
    	        
    	          //  System.out.print((this.getX()-(r.nextInt(movelen)-movelen/2))+""+(this.getY()-(r.nextInt(movelen)-movelen/2)));
//    	            inmove=false;*/
            //   teamv.get(i).translate(x, y);
             //  Point2D.Double target = new Point2D.Dpouble( teamv.get(i).getX()+x0,  teamv.get(i).getY()+y0);
            //  teamv.get(i).setDirection(target);
            //   teamv.get(i).move(step);
           }    
           //环绕
           
       }
       
          else if(movetype==3)
       {
                 int i;
           if(teamv.get(0).distance(new Point2D.Double(remx,remy))>step&&inited==true)
           {
               
                for(i=0;i<teamv.size();i++)
              {
               
                    teamv2.get(i).inmove=true;
                 teamv2.get(i).move(0.3);
                   teamv.get(i).move(0.3);
              }
               return;
           } 
    	  
          inited=true;
           
     
           Random ran=new Random();
           
            double tx=teamv.get(0).getX();
              double ty=teamv.get(0).getY();
              
                double l=Math.sqrt(Math.pow(tx-x,2)+Math.pow(ty-y,2));
                if(lmax==-1)
                {
                    lmax=l;
                }
                else
                {
                    l=lmax;
                }
                 double rx,dx;
                   double ry,dy;
                while(true)
                {
                    rx=(l*2)*(ran.nextDouble()-0.5);
                    ry=(l*2)*(ran.nextDouble()-0.5);
                    double ln=Math.sqrt(Math.pow(rx,2)+Math.pow(ry,2));
                    if(ln<l)
                    {
                        remx=rx+x;
                        remy=ry+y;
                        
                        dx=remx-tx;
                        dy=remy-ty;
                        break;
                    }
                }
              for(i=0;i<teamv.size();i++)
              {
                  Point2D.Double target = new Point2D.Double(teamv.get(i).getX()+dx, teamv.get(i).getY()+dy);
                   
                   teamv.get(i).setDirection(target);
                   teamv.get(i).move(0.3);
                     teamv2.get(i).setDirection(target);
                   teamv2.get(i).move(0.3);
              }
              
            //   teamv.get(i).translate(x, y);
             //  Point2D.Double target = new Point2D.Dpouble( teamv.get(i).getX()+x0,  teamv.get(i).getY()+y0);
            //  teamv.get(i).setDirection(target);
            //   teamv.get(i).move(step);
              int mm=1;
           }    
       
       
          else if(movetype==99)
       {
           
           int i=0;
           boolean find=false;
           double tt;
           for(i=0;i<teamv.size();i++)
           {
               // teamv2.get(i).inmove=true;
                 tt=teamv.get(i).distance(targets.get(i));
               if(tt>1.1*step)
               {
                    teamv2.get(i).move(0.3);
                     teamv.get(i).move(0.3);
                     find=true;
               }
               else
               {
                   
               }
           }
           if(find==false)
           {
               movetype=0;
                global.jbm.donext(index);
           }
              
    	  
      
     
           }  
           //环绕
           
       }
   }


